﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:apexControls="clr-namespace:Apex.Controls"
    xmlns:apexCommands="clr-namespace:Apex.Commands"
    xmlns:apexConverters="clr-namespace:Apex.Converters">

    <Style TargetType="{x:Type apexControls:PivotControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type apexControls:PivotControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <Grid>
                            <Grid.Resources>
                                <apexConverters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
                            </Grid.Resources>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <ItemsControl 
                                Grid.Row="0"
                                ItemsSource="{TemplateBinding PivotItems}"
                                Visibility="{TemplateBinding ShowPivotHeadings,
                                Converter={StaticResource BooleanToVisibilityConverter}}"
                                HorizontalAlignment="Center">

                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock 
                                            x:Name="tbPivotTitle" Margin="6,0,6,0" FontSize="32" Text="{Binding Title}" Opacity="0.9" Foreground="#FF797979" 
                                                    
                                            apexCommands:ExtendedCommands.LeftClickCommand="{Binding SelectPivotItemCommand, RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type apexControls:PivotControl}}}" 
                                                    apexCommands:ExtendedCommands.LeftClickCommandParameter="{Binding}">
                                        </TextBlock>

                                        <DataTemplate.Resources>
                                            <Storyboard x:Key="FadeIn">
                                                <DoubleAnimation BeginTime="00:00:00" Duration="0:0:0.1" To="0.9" Storyboard.TargetProperty="(TextBlock.Opacity)" />
                                            </Storyboard>
                                            <Storyboard x:Key="FadeOut">
                                                <DoubleAnimation BeginTime="00:00:00" Duration="0:0:0.3" To="0.3" Storyboard.TargetProperty="(TextBlock.Opacity)" />
                                            </Storyboard>
                                        </DataTemplate.Resources>

                                        <DataTemplate.Triggers>
                                            <DataTrigger  Binding="{Binding IsSelected}" Value="True">
                                                <Setter TargetName="tbPivotTitle" Property="Foreground" Value="#FF000000"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding IsSelected}" Value="False">
                                                <Setter TargetName="tbPivotTitle" Property="Foreground" Value="#FF797979"/>
                                            </DataTrigger>
                                            <Trigger Property="IsMouseOver" Value="True">

                                                <Trigger.EnterActions>
                                                    <BeginStoryboard Storyboard="{StaticResource FadeIn}"/>
                                                </Trigger.EnterActions>

                                                <Trigger.ExitActions>
                                                    <BeginStoryboard Storyboard="{StaticResource FadeOut}"/>
                                                </Trigger.ExitActions>

                                            </Trigger>
                                        </DataTemplate.Triggers>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>

                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                            <apexControls:AnimatedScrollViewer 
                                x:Name="PART_PivotScrollViewer" 
                                Grid.Row="1" VerticalScrollBarVisibility="Hidden" 
                                HorizontalScrollBarVisibility="Hidden"
                                HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                <ItemsControl 
                                    x:Name="PART_ItemsControl" 
                                    ItemsSource="{TemplateBinding PivotItems}" HorizontalAlignment="Center">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal" Margin="1000, 0, 1000, 0" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Grid x:Name="parentGrid" Margin="200, 0, 200, 0">
                                                <ContentPresenter 
                                                   DataContext="{Binding DataContext,
                                                    RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type apexControls:PivotControl}}}" 
                                                    Content="{Binding DataContext.Content,
                                                    RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type Grid}}}"  />
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </apexControls:AnimatedScrollViewer>
                        </Grid>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>